package modelo;

import hbt.AbstractHibernateManager;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import entities.CajaEntity;
import entities.CartaEntity;

public class CajaDao  extends AbstractHibernateManager {

	private static CajaDao instancia = null;
	
	public static CajaDao getInstancia() {
		if(instancia == null) {
			instancia = new CajaDao();
		}
		return instancia;
	}
		public void merge (CajaEntity caja){
			Session s = null;
			try {
				s=getNewSession();
				s.beginTransaction();
				s.merge(caja);
				s.flush();
				s.getTransaction().commit();
				System.out.println("[Caja SAVED ON TABLE cajas]::OK");
			} catch (HibernateException e) {
				e.printStackTrace();
			} finally{
				closeSession(s);
			}
		}
		
		public CajaEntity obtenerCaja(Integer idCaja) {
			Session s = null;
			CajaEntity caja = new CajaEntity();
			
			try {
				s = getNewSession();
				caja =  (CajaEntity) s.createQuery("FROM CajaEntity caja " +
										"WHERE caja.id = :idCaja")
								  .setInteger("idCaja", idCaja)
								  .uniqueResult();
				
			} catch (HibernateException e) {
				e.printStackTrace();
			} finally{
				closeSession(s);
			}
			return caja;
		}

}
